package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.common.PageResult;
import com.dongdongshop.mapper.GoodsESMapper;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@DubboService
public class TbGoodsServiceImpl implements TbGoodsService {
    @Resource
    private TbGoodsMapper tbGoodsMapper;
    @Resource
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Resource
    private TbItemMapper  tbItemMapper;
    @Resource
    private GoodsESMapper goodsESMapper;
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Override
    public PageResult<TbGoods> selectInfoLimitServer(TbGoods goods, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<TbGoods> list = tbGoodsMapper.selectByGoodsName(goods);
        PageInfo<TbGoods> page = new PageInfo<>(list);
        PageResult<TbGoods> pageResult = new PageResult<>(page.getTotal(),page.getList());
        return pageResult;
    }

    @Override
    @Transactional//保持事务的一致性
    public int addGoodsAndDesc(TbGoods goods, TbGoodsDesc goodsDesc, String listItem) {
        /*添加tb_goods表并返回Id*/
        int i =  tbGoodsMapper.addGoodsReturnId(goods);
        goodsDesc.setGoodsId(goods.getId());
        /*添加tb_goods_desc表*/
        int j = tbGoodsDescMapper.insertSelective(goodsDesc);
        /*添加tb_item表*/
        List list = JSONObject.parseObject(listItem, List.class);
        for (Object o : list) {
            System.out.println(o);
        }
        List<ItemVO> itemVO = JSONObject.parseArray(listItem, ItemVO.class);
        for (int q = 0; q < itemVO.size(); q++) {
            ItemVO vo = itemVO.get(q);
            TbItem tbItem = new TbItem();
            tbItem.setTitle(goods.getGoodsName()+"-"+vo.getNetwork() + vo.getMemorys());
            tbItem.setPrice(vo.getPrice());
            tbItem.setNum(vo.getNum());
            tbItem.setStatus(vo.getStatus());
            tbItem.setIsDefault(vo.getIsDefault());
            tbItem.setCategoryid(goods.getCategory3Id());
            tbItem.setCreateTime(new Date());
            tbItem.setUpdateTime(new Date());
            int l = tbItemMapper.insert(tbItem);
        }
        return i+j;
    }

    @Override
    public TbGoods selectByGoodsId(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public int getCommodityAuditIds(Long[] goodIds) {
        int i = tbGoodsMapper.updateByGoodIdsAdopt(goodIds);
        return i;
    }

    @Override
    public int getCommodityFailIds(Long[] goodIds) {
        int i = tbGoodsMapper.updateByGoodIdsFail(goodIds);
        return i;
    }


    @Override
    public PageResult<TbGoodsES> selectInfoLimitServerES(TbGoods goods, Integer pageNumber, Integer pageSize) {
        List<TbGoodsES> items = new ArrayList<>();
        PageResult<TbGoodsES> pageResult = null;
        if (goods.getGoodsName()!= null && !"".equals(goods.getGoodsName())){
            List<SearchHit<TbGoodsES>> byGoodsName = goodsESMapper.findByGoodsName(goods.getGoodsName(),PageRequest.of(pageNumber - 1,pageSize));
            for (SearchHit<TbGoodsES> searchHit : byGoodsName) {
                TbGoodsES item = searchHit.getContent();
//            System.out.println("非高亮数据"+item);
                List<String> listHlight = searchHit.getHighlightField("goodsName");
//            System.out.println(listHlight);
                //有高亮数据
                if(listHlight!=null&&listHlight.size()>0){
                    item.setGoodsName(listHlight.get(0));
                }
//            System.out.println("包含高亮数据"+item);
                items.add(item);
            }
            items.forEach(System.out::println);
            List<SearchHit<TbGoodsES>> byGoodsName1 = goodsESMapper.findByGoodsName(goods.getGoodsName());
            pageResult = new PageResult<>(Long.valueOf(byGoodsName1.size()),items);
            return pageResult;
        }else {
            Iterable<TbGoodsES> all = goodsESMapper.findAll(PageRequest.of(pageNumber-1,pageSize));
            all.forEach(tbs->items.add(tbs));
            Long num = 0L;
            Iterable<TbGoodsES> all1 = goodsESMapper.findAll();
            for (TbGoodsES tbGoodsES : all1) {
                num++;
            }
            pageResult = new PageResult<>(num,items);
            return pageResult;
        }

    }

}
